package com.yc.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.study.entity.Chapter;
import com.yc.study.service.IChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 章节 前端控制器
 *
 * @author 王妍
 * @since 2022-05-05
 */
@RestController
@RequestMapping("/study/chapter")
public class ChapterController {

  @Autowired private IChapterService chapterService;

  @PostMapping
  public void save(@RequestBody Chapter chapter) {
    chapterService.saveOrUpdate(chapter);
  }

  @GetMapping("info/{chapterId}")
  public Chapter getInfo(@PathVariable Long chapterId) {
    return chapterService.getById(chapterId);
  }

  @GetMapping("{courseId}")
  public List<Chapter> chapterList(@PathVariable Long courseId) {
    return chapterService.list(
        new LambdaQueryWrapper<Chapter>()
            .eq(Chapter::getCourseId, courseId)
            .orderByAsc(Chapter::getSort)
            .orderByAsc(Chapter::getName));
  }

  @DeleteMapping
  public void deleteByIds(@RequestBody Long[] ids) {
    chapterService.removeByIds(Arrays.asList(ids));
  }
}
